package cn.ljp.shopComment.service.service.impl;

import cn.cc.common.beanCopy.utils.BeanCopierUtil;
import cn.cc.common.result.utils.RestResult;
import cn.cc.common.result.utils.RestResultEmum;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import cn.ljp.shopComment.mapper.mapper.SmallMiUserCommentMapper;
import cn.ljp.shopComment.mapper.mapper.SmallMiUserMapper;
import cn.ljp.shopComment.mapper.pojo.SmallMiUser;
import cn.ljp.shopComment.mapper.pojo.SmallMiUserComment;
import cn.ljp.shopComment.service.bo.DeleteShopCommentBo;
import cn.ljp.shopComment.service.bo.InsertShopCommentBo;
import cn.ljp.shopComment.service.bo.SeleceShopCommentBo;
import cn.ljp.shopComment.service.domain.DeleteShopCommentDo;
import cn.ljp.shopComment.service.domain.InsertShopCommentDo;
import cn.ljp.shopComment.service.domain.SeleceShopCommentDo;
import cn.ljp.shopComment.service.service.ShopCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述类
 *
 * @ClassName ShopCommentServiceImpl
 * @Description TODO
 * @Author liu'jia'peng
 * @Date 2021/7/20 17:20
 * @Version 1.0
 */
@Service
public class ShopCommentServiceImpl implements ShopCommentService {

    @Autowired
    private SmallMiUserCommentMapper smallMiUserCommentMapper;

    @Override
    public InsertShopCommentDo insertShopComment(InsertShopCommentBo bo) {
        SmallMiUserComment smallMiUserComment = BeanCopierUtil.copyObj(bo, SmallMiUserComment.class);
        smallMiUserComment.setCreationDate(new Date());
        smallMiUserComment.setCommentId(new ObjectIdGenerator().next());
        int i = smallMiUserCommentMapper.insertSelective(smallMiUserComment);
        InsertShopCommentDo insertShopCommentDo = new InsertShopCommentDo();
        insertShopCommentDo.setToken(bo.getToken());
        if (1==i){
            insertShopCommentDo.setCode(RestResultEmum.SUCCESS.code);
            return insertShopCommentDo;//返回成功的
        }
        insertShopCommentDo.setCode(RestResultEmum.SAVE_ERROR.code);
        return insertShopCommentDo;//返回执行失败的
    }

    @Override
    public DeleteShopCommentDo deleteShopComment(DeleteShopCommentBo bo) {
        SmallMiUserComment smallMiUserComment = BeanCopierUtil.copyObj(bo, SmallMiUserComment.class);
        int i = smallMiUserCommentMapper.deleteShopComment(smallMiUserComment);
        DeleteShopCommentDo deleteShopCommentDo = new DeleteShopCommentDo();
        deleteShopCommentDo.setToken(bo.getToken());
        if (1==i){
            deleteShopCommentDo.setCode(RestResultEmum.SUCCESS.code);
            return deleteShopCommentDo;//返回成功的
        }
        deleteShopCommentDo.setCode(RestResultEmum.DEL_ERROR.code);
        return deleteShopCommentDo;//返回执行失败的
    }

    @Autowired
    private SmallMiUserMapper smallMiUserMapper;

    @Override
    public List<SeleceShopCommentDo> seleceShopCommentFat(SeleceShopCommentBo bo) {
        SmallMiUserComment smallMiUserComment = BeanCopierUtil.copyObj(bo, SmallMiUserComment.class);
        //查询字段 ， 评论id 用户id 父类id  评论内容
        List<SmallMiUserComment> smallMiUserComments = smallMiUserCommentMapper.selectAllShopComment(smallMiUserComment);
        //返回参数，用户id 用户头像，用户名称 评论内容
        List<SeleceShopCommentDo> list = new ArrayList<>();
        for (SmallMiUserComment miUserComment : smallMiUserComments) {
            //先遍历父亲
            if (miUserComment.getParentId().equals("0")){
                SeleceShopCommentDo seleceShopCommentDo = new SeleceShopCommentDo();
                seleceShopCommentDo.setCommentContent(miUserComment.getCommentContent());
                seleceShopCommentDo.setThumbNum(miUserComment.getThumbNum());
                //查询用户信息
                SmallMiUser smallMiUser = new SmallMiUser();
                smallMiUser.setMiId(miUserComment.getMiId());
                List<SmallMiUser> smallMiUsers = smallMiUserMapper.selectUserShopComment(smallMiUser);
                for (SmallMiUser miUser : smallMiUsers) {
                    seleceShopCommentDo.setMiId(miUser.getMiId());
                    seleceShopCommentDo.setMiName(miUser.getMiName());
                    seleceShopCommentDo.setMiIconUrl(miUser.getMiIconUrl());
                    list.add(seleceShopCommentDo);
                }
            }
        }
        return list;
    }

    @Override
    public List<SeleceShopCommentDo> seleceShopCommentSon(SeleceShopCommentBo bo) {
        SmallMiUserComment smallMiUserComment = BeanCopierUtil.copyObj(bo, SmallMiUserComment.class);
        //查询字段 ， 评论id 用户id 父类id  评论内容
        List<SmallMiUserComment> smallMiUserComments = smallMiUserCommentMapper.selectAllShopComment(smallMiUserComment);
        //返回参数，用户id 用户头像，用户名称 评论内容
        List<SeleceShopCommentDo> list = new ArrayList<>();
        for (SmallMiUserComment miUserComment : smallMiUserComments) {
            //先遍历父亲
            if (!miUserComment.getParentId().equals("0")){
                SeleceShopCommentDo seleceShopCommentDo = new SeleceShopCommentDo();
                seleceShopCommentDo.setCommentContent(miUserComment.getCommentContent());
                seleceShopCommentDo.setThumbNum(miUserComment.getThumbNum());
                seleceShopCommentDo.setParentId(miUserComment.getParentId());
                //查询用户信息
                SmallMiUser smallMiUser = new SmallMiUser();
                smallMiUser.setMiId(miUserComment.getMiId());
                List<SmallMiUser> smallMiUsers = smallMiUserMapper.selectUserShopComment(smallMiUser);
                for (SmallMiUser miUser : smallMiUsers) {
                    seleceShopCommentDo.setMiId(miUser.getMiId());
                    seleceShopCommentDo.setMiName(miUser.getMiName());
                    seleceShopCommentDo.setMiIconUrl(miUser.getMiIconUrl());
                    list.add(seleceShopCommentDo);
                }
            }
        }
        return list;
    }

}

